import Vue from 'vue';
import Router from 'vue-router';
import store from 'src/store';
import { userStorageName } from 'common/js/config';
import { getLocalStorage } from 'common/js/dom';

Vue.use(Router);

//入口
const Main = () => import ('components/Main/Main');

//登录
const Login = () => import ('components/Login/Login');

//首页
const Home = () => import ('components/Home/Home');

//应用系统管理
const Application = () => import ('components/Application/Application');

//接口管理
const Interface = () => import ('components/Interface/Interface');

//修改日志
const ModificationLog = () => import ('components/ModificationLog/ModificationLog');

//接口请求日志
const InterfaceLog = () => import ('components/InterfaceLog/InterfaceLog');

//应用系统服务
const ApplicationService = () => import ('components/ApplicationService/ApplicationService');

//服务日志
const ServiceLog = () => import ('components/ServiceLog/ServiceLog');

//任务管理
const Task = () => import ('components/Task/Task');

//数据源管理
const DataSource = () => import ('components/DataSource/DataSource');

//任务配置
const TaskConfig = () => import ('components/TaskConfig/TaskConfig');

//文档管理
const Document = () => import ('components/Document/Document');

//服务器监控
const ServiceMonitor = () => import ('components/ServiceMonitor/ServiceMonitor');

//Redis监控
const RedisMonitor = () => import ('components/RedisMonitor/RedisMonitor');

//用户管理
const User = () => import ('components/User/User');

//角色管理
const Role = () => import ('components/Role/Role');

const router = new Router({
	routes: [{
		path: '*',
		redirect: '/'
	}, {
		path: '/',
		component: Main
	}, {
		path: '/login',
		component: Login
	}, {
		path: '/home',
		component: Home,
		redirect: '/home/Application',
		children: [{
			path: '/home/Application',
			component: Application
		}, {
			path: '/home/Interface',
			component: Interface
		}, {
			path: '/home/ModificationLog',
			component: ModificationLog
		}, {
			path: '/home/InterfaceLog',
			component: InterfaceLog
		}, {
			path: '/home/ApplicationService',
			component: ApplicationService
		}, {
			path: '/home/ServiceLog',
			component: ServiceLog
		}, {
			path: '/home/Task',
			component: Task
		}, {
			path: '/home/DataSource',
			component: DataSource
		}, {
			path: '/home/TaskConfig',
			component: TaskConfig
		}, {
			path: '/home/Document',
			component: Document
		}, {
			path: '/home/ServiceMonitor',
			component: ServiceMonitor
		}, {
			path: '/home/RedisMonitor',
			component: RedisMonitor
		}, {
			path: '/home/User',
			component: User
		}, {
			path: '/home/Role',
			component: Role
		}]
	}]
})

router.beforeEach((to, from, next) => {
    if (to.path === '/' || to.path === '/login') {
    	next()
    }else {
    	const User = getLocalStorage(userStorageName);
    	if (User) {
    		store.commit('SET_USER', User);
    		next()
    	}else {
    		next('/login');
    	}
    }
    
})

export default router